package com.ydgk.jishuzhan.controller;

import com.ydgk.jishuzhan.beans.*;
import com.ydgk.jishuzhan.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class DispatcherController {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private TalkService talkService;



    @RequestMapping(value = "/doLogin" , method = RequestMethod.POST)
    public String doLogin(User user,HttpSession session,Model model){
        User user1=userService.login(user);
        List<Blog> blogs = blogService.findBlogs();
        List<Question> questions = questionService.findQuestions();
        List<Talk> talks=talkService.findTalks();
        model.addAttribute("blogs",blogs);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        System.out.println(user1);
         if (user1!=null){
             session.setAttribute("user", user1);
            return "index";
        }else {
//             modelMap.addAttribute("errorMsg", "用户名或密码错误！");
             return "redirect:/toLogin";
         }
    }

    @RequestMapping("/toIndex")
    public String toIndex(Model model){
        List<Blog> blogs = blogService.findBlogs();
        List<Question> questions = questionService.findQuestions();
        List<Talk> talks=talkService.findTalks();
        model.addAttribute("blogs",blogs);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        return "index";
    }


    @RequestMapping(value = "/toCollection", method = RequestMethod.GET)
    public String toIndex(@RequestParam(value = "pageNo1", required = false, defaultValue = "1") Integer pageNo1,
                          @RequestParam(value ="pageNo2" ,required = false, defaultValue = "1" ) Integer pageNo2,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                          @RequestParam(value= "userId" ,required = false ,defaultValue="1") Integer userId,
                          ModelMap modelMap) {

        Integer pageStart1 = (pageNo1 - 1) * pageSize;
        Integer pageStart2 = (pageNo2 - 1) * pageSize;

        // 将请求参数封装成一个map
        Map<String, Object> map = new HashMap<>();
        map.put("start1", pageStart1);
        map.put("start2",pageStart2);
        map.put("size", pageSize);
        map.put("userId",userId);

        // 获取用户数据

        List<BlogCollection> blogCollections=blogService.getCollections(map);
        List<Blog> blogs=new ArrayList<>();
        for(BlogCollection b: blogCollections){
            blogs.add(blogService.findBlog(b.getBlogId()));
        }

        List<QuestionCollection> questionCollections=questionService.getCollections(map);
        List<Question> questions=new ArrayList<>();
        for(QuestionCollection q: questionCollections){
            questions.add(questionService.findQuestion(q.getQuestionId()));
        }
        // 将获取到的数据存入到 模型中
        //modelMap.addAttribute("messages",messages);
        //将页面中需要的数据都传入到模型中   比如  pageNo   pageTotal  messages

        // 页码的总数   ===>  数据的条数    /   页面的大小   == > 页码的总数
        Integer pageTotal1 = 0;
        Integer pageTotal2=0;

        // 数据的条数

        Integer BlogTotal=blogService.queryBlogTotal(map);
        Integer QuestionTotal=questionService.queryQuestionTotal(map);

        if (BlogTotal % pageSize==0){
            pageTotal1=BlogTotal / pageSize;
        }else {
            pageTotal1=BlogTotal / pageSize + 1;
        }

        if (QuestionTotal % pageSize==0){
            pageTotal2=QuestionTotal / pageSize;
        }else {
            pageTotal2=QuestionTotal / pageSize + 1;
        }

        // 将数据封装到模型中
//        modelMap.addAttribute("messages",messages);
//        modelMap.addAttribute("pageNo",pageNo);
//        modelMap.addAttribute("pageTotal",pageTotal);

        // 构建一个对象，将需要的数据封装到对象中，传入页面时直接传入对象
        Page<Blog> page1 = new Page(blogs, pageNo1, pageTotal1);
        Page<Question> page2=new Page (questions ,pageNo2,pageTotal2);

        modelMap.addAttribute("page1",page1);
        modelMap.addAttribute("page2",page2);
        modelMap.addAttribute("BlogTotal",BlogTotal);
        modelMap.addAttribute("QuestionTotal",QuestionTotal);

        return "collection";
    }
}
